package com.wiken.commentservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wiken.commentservice.DTO.CommentStoreDTO;
import com.wiken.commentservice.DTO.CommentUserDTO;
import com.wiken.commentservice.VO.StoreCommodityVO;
import com.wiken.commentservice.entity.Comment;
import com.wiken.commentservice.feign.OrderServiceClient;
import com.wiken.commentservice.mapper.CommentMapper;
import com.wiken.commentservice.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wiken.servicecommon.common.Code;
import com.wiken.servicecommon.common.PageUtill;
import com.wiken.servicecommon.common.TokenUtil;
import com.wiken.servicecommon.common.exception.DefaultException;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wiken
 * @since 2022-12-28
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Autowired
    OrderServiceClient orderServiceClient;

    boolean hasSameUser(Comment comment, String token){
        Claims claims = TokenUtil.parseToken(token);

        if(claims == null) throw new DefaultException(Code.ERROR_AUTHORIZATION_EXPIRATION, "token过期");
        String userId = claims.get("userId", String.class);

        if(userId == null) throw new DefaultException(Code.ERROR_AUTHORIZATION_NULL, "账号异常");

        if(!userId.equals(comment.getUserId())) throw new DefaultException(Code.ERROR_AUTHORIZATION, "操作异常");

        return true;
    }
    @Override
    public boolean addComment(Comment comment, String token) {
        hasSameUser(comment, token);

        //判断这个商品我是否购买过
        String userId = baseMapper.getUserIdByOrderCommodityId(comment.getOrderCommodityId(), comment.getUserId());

        if(userId == null) throw new DefaultException(Code.ERROR_AUTHORIZATION, "评论失败");

        //修改OrderCommodity状态为1
        boolean ok = orderServiceClient.updateOrderCommodityStatus(comment.getOrderCommodityId(), 1);

        if(!ok) throw new DefaultException(Code.ERROR_ORDER_COMMODITY_UPDATE, "评论失败");

        int success = baseMapper.insert(comment);
        return success > 0;
    }

    @Override
    public boolean updateComment(Comment comment, String token) {
        hasSameUser(comment, token);

        int success = baseMapper.updateById(comment);

        return success > 0;
    }

    @Override
    public boolean delComment(Comment comment, String token) {
        hasSameUser(comment, token);

        int success = baseMapper.deleteById(comment);
        return success > 0;
    }

    @Override
    public Page<CommentStoreDTO> getMyComment(Comment comment, PageUtill pageUtill, String token) {
        hasSameUser(comment, token);

        Page page = new Page<>();
        page.setCurrent(pageUtill.getCurrent());
        page.setSize(pageUtill.getSize());

        Page<CommentStoreDTO> commentList = baseMapper.getCommentByUserId(comment.getUserId(), page);
        return commentList;
    }

    @Override
    public Page<CommentUserDTO> getCommentByStoreId(String storeId, String token, PageUtill pageUtill) {
        Page page = new Page<>();
        page.setCurrent(pageUtill.getCurrent());
        page.setSize(pageUtill.getSize());

        String userId = null;
        Claims claims = null;

        if(null != token) claims = TokenUtil.parseToken(token);

        if(null != claims) userId = claims.get("userId", String.class);


        Page<CommentUserDTO> commentList = baseMapper.getCommentByStoreId(storeId, userId, page);
        return commentList;
    }

    @Override
    public Page<StoreCommodityVO> getCommentOfNeedComment(String token, PageUtill pageUtill) {
        if(token == null) throw new DefaultException(Code.ERROR_AUTHORIZATION_NULL, "请重新登陆");
        Claims claims = TokenUtil.parseToken(token);

        if(claims == null) throw new DefaultException(Code.ERROR_AUTHORIZATION_EXPIRATION, "请重新登陆");

        String userId = claims.get("userId", String.class);
        if(userId == null) throw new DefaultException(Code.ERROR_AUTHORIZATION_NULL, "请重新登陆");

        Page page = new Page();
        page.setCurrent(pageUtill.getCurrent());
        page.setSize(pageUtill.getSize());


        Page<StoreCommodityVO> StoreCommodityVO = baseMapper.getCommentOfNeedComment(userId, page);

        return StoreCommodityVO;
    }

    @Override
    public Page<StoreCommodityVO> getCommentOfCommented(String token, PageUtill pageUtill) {
        if(token == null) throw new DefaultException(Code.ERROR_AUTHORIZATION_NULL, "请重新登陆");

        Claims claims = TokenUtil.parseToken(token);

        if(claims == null) throw new DefaultException(Code.ERROR_AUTHORIZATION_EXPIRATION, "请重新登陆");

        String userId = claims.get("userId", String.class);
        if(userId == null) throw new DefaultException(Code.ERROR_AUTHORIZATION_NULL, "请重新登陆");

        Page page = new Page();
        page.setCurrent(pageUtill.getCurrent());
        page.setSize(pageUtill.getSize());

        Page<StoreCommodityVO> StoreCommodityVO = baseMapper.getCommentOfCommented(userId, page);

        return StoreCommodityVO;
    }
}
